package com.dms.modules.message.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.api.ApiResponse;
import com.dms.common.security.SecurityUtils;
import com.dms.modules.message.dto.MessageDTO;
import com.dms.modules.message.dto.MessageDetailDTO;
import com.dms.modules.message.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;

@Slf4j
@Validated
@RestController
@RequestMapping("/api/messages")
@Api(tags = "消息管理")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping
    @ApiOperation("获取消息列表")
    public ApiResponse<IPage<MessageDetailDTO>> getMessages(
            @RequestParam(defaultValue = "1") @Min(value = 1, message = "页码不能小于1") Integer pageNum,
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页条数不能小于1") Integer pageSize,
            @RequestParam(required = false) Integer type) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            Page<MessageDetailDTO> page = new Page<>(pageNum, pageSize);
            IPage<MessageDetailDTO> messages = messageService.getUserMessages(userId, type, page);
            return ApiResponse.success(messages);
        } catch (Exception e) {
            log.error("获取消息列表失败: pageNum={}, pageSize={}, type={}, error={}", 
                    pageNum, pageSize, type, e.getMessage(), e);
            return ApiResponse.error(e);
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("获取消息详情")
    public ApiResponse<MessageDetailDTO> getMessageDetail(@PathVariable Long id) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            MessageDetailDTO detail = messageService.getMessageDetail(userId, id);
            if (detail == null) {
                return ApiResponse.notFound("消息不存在");
            }
            return ApiResponse.success(detail);
        } catch (Exception e) {
            log.error("获取消息详情失败: id={}, error={}", id, e.getMessage(), e);
            return ApiResponse.error(e);
        }
    }

    @PutMapping("/{id}/read")
    @ApiOperation("标记消息已读")
    public ApiResponse<Boolean> markAsRead(@PathVariable Long id) {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            boolean result = messageService.markAsRead(userId, id);
            return ApiResponse.success(result);
        } catch (Exception e) {
            log.error("标记消息已读失败: id={}, error={}", id, e.getMessage(), e);
            return ApiResponse.error(e);
        }
    }

    @GetMapping("/unread/count")
    @ApiOperation("获取未读消息数")
    public ApiResponse<Integer> getUnreadCount() {
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            int count = messageService.getUnreadCount(userId);
            return ApiResponse.success(count);
        } catch (Exception e) {
            log.error("获取未读消息数失败: error={}", e.getMessage(), e);
            return ApiResponse.error(e);
        }
    }
} 